namespace BM.Tools.WebControls.MForm.Navigation.XPath
{    
    using System.Collections.ObjectModel;

    /// <summary>
    /// The definition of the xpath-like path.
    /// </summary>
    public class Path
    {
        private Collection<LocationStep> locationSteps = new Collection<LocationStep>();

        /// <summary>
        /// Initializes a new instance of the <see cref="Path"/> class. 
        /// </summary>
        /// <param name="pathText">
        /// The text representation of the path.
        /// </param>
        public Path(string pathText)
        {
            foreach (string pathPart in pathText.Split('/'))
            {
                AddLocationStep(LocationStep.Create(pathPart));
            }
        }

        /// <summary>
        /// Gets LastLocationStep.
        /// </summary>
        /// <value>
        /// The last location step.
        /// </value>
        public LocationStep LastLocationStep
        {
            get
            {
                if (LocationSteps.Count > 0)
                {
                    return LocationSteps[LocationSteps.Count - 1];
                }

                return null;
            }
        }

        /// <summary>
        /// Gets LocationSteps.
        /// </summary>
        /// <value>
        /// The location steps.
        /// </value>
        public Collection<LocationStep> LocationSteps
        {
            get
            {
                return locationSteps;
            }
        }

        private void AddLocationStep(LocationStep locationStep)
        {
            if (LastLocationStep != null && LastLocationStep.DenotesDescendantOrSelf && locationStep != null
                && locationStep.DenotesDescendantOrSelf)
            {
                return;
            }

            LocationSteps.Add(locationStep);
        }
    }
}